﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="AddEditUser.aspx.cs" Inherits="TRL.TRLAdmin.UserMgmt.AddEditUser" Trace="false" %>
<%@ Register TagPrefix="uc" TagName="ConfirmDeleteModal" Src="ConfirmDeleteModal.ascx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <nav>
        <div id="jCrumbs" class="breadCrumb module">
            <ul>
                <li>
                    <a href="#"><i class="icon-home"></i></a>
                </li>
                <li>
                    <a href="ViewUsers.aspx">Admin User Management</a>
                </li>
                <li>
                <%
                    if (OperationMode == Mode.EditUser)
                    {
                        Response.Write("Edit User: " + UserFullName);
                    }
                    else
                    {
                        Response.Write("Add User");
                    }
                %>
                </li>
            </ul>
        </div>
    </nav>

    <div class="page-header">
        <%
            if (OperationMode == Mode.EditUser)
            {
                Response.Write("<h1>Edit User <small>" + UserFullName + "</small></h1>");
            }
            else
            {
                Response.Write("<h1>Add User</h1>");
            } 
        %>
    </div>
    
    <div class="row">
        <div class="span4">
            <asp:Label ID="message" runat="server" style="display: block" Visible="false" EnableViewState="false"></asp:Label>
        </div>
    </div>

    <div class="row">
        <div class="span8">
            <asp:FormView ID="AddEditUserFormView" runat="server" DataKeyNames="UserId" 
                DataSourceID="AddEditUserSQLDataSource" 
                EnableViewState="false"
                CssClass="well"
                DefaultMode="Insert">
                <EditItemTemplate>
                    <fieldset class="form-horizontal addEditMemberForm">
                        <div class="control-group">
                            <label class="control-label">Full Name</label>
                            <div class="controls">
                                <asp:TextBox ID="FullName" runat="server" Text='<%# Bind("fullName") %>' class="required" />
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">Email</label>
                            <div class="controls">
                                <asp:TextBox ID="Email" runat="server" Text='<%# Bind("Email") %>' class="required email" />
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">Username</label>
                            <div class="controls">
                                <asp:TextBox ID="Username" runat="server" Text='<%# Bind("Username") %>' class="required" />
                            </div>
                        </div>


                        <div class="control-group">
                            <label class="control-label">Password</label>
                            <div class="controls">
                                <asp:TextBox ID="Password" runat="server" Text="" type="password" ClientIDMode="Static" />
                            </div>
                        </div>


                        <div class="control-group">
                            <label class="control-label">Confirm Password</label>
                            <div class="controls">
                                <asp:TextBox ID="ConfirmPassword" runat="server" Text="" type="password" ClientIDMode="Static"  />
                            </div>
                        </div>
                    </fieldset>
                    <asp:Button Visible="true" ID="AddButton" runat="server" Text="Add User" 
                        CssClass="btn btn-primary" CausesValidation="True" 
                        OnClick="AddButton_Click" />

                    <asp:Button ID="UpdateButton" runat="server" Text="Update User" 
                        CssClass="btn btn-primary" CausesValidation="True" Visible="false"
                        OnClick="UpdateButton_Click" />

                    <asp:Button ID="CancelButton" runat="server" Text="Cancel" 
                        CssClass="btn btn-danger cancel" CausesValidation="False" Visible="true" 
                        OnClick="CancelButton_Click" />
                    
                    <div style="margin-top:10px">
                        <asp:Button ID="DeleteButton" runat="server" Text="Delete User" 
                            CssClass="btn btn-inverse" CausesValidation="False" Visible="false"
                            UseSubmitBehavior="true"
                            />
                    </div>
                </EditItemTemplate>
            </asp:FormView>
            <%--            
                Note that we only use the SqlDataSource to populate the FormView on edit.
                We implement are own handlers for inserting and udpating users as we have  
                to call the MembershipProvider API in addition to SQL.
            --%>
            <asp:SqlDataSource ID="AddEditUserSQLDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:trladminConnectionString %>" 
                SelectCommand="
                    SELECT 
                        u.userId as UserId, u.Username, au.fullName, u.Email 
                    FROM [dbo].[vw_aspnet_MembershipUsers] u
                    LEFT JOIN AdminUserInfo au ON
                        u.UserId = au.userID
                    WHERE 
                        u.userId = @userId">
                    <SelectParameters>
                        <%-- 
                            Implementation note: We don't use the QueryStringParamater control as UserId can be provided by either a
                            querystring or viewstate. 
                        --%>
                        <asp:ControlParameter Name="userId" ControlID="__Page" PropertyName="UserId"/>
                    </SelectParameters>
            </asp:SqlDataSource>
        </div>
    </div>

    <uc:ConfirmDeleteModal ID="ConfirmDeleteModal1" runat="server"></uc:ConfirmDeleteModal>
</asp:Content>


<asp:Content ID="Content3" ContentPlaceHolderID="scripts" runat="server">
    <script type="text/javascript">
    $(document).ready(function () {
        // Set the sidebar orientation to the section and page 
        // of this document
        $('#collapseThree').addClass("in");

        <%
            // Highlight in the sidebar the approriate page based on the mode we are in
            if (OperationMode == Mode.AddUser)
                Response.Write("$('#addUser').addClass(\"active\");");
            else 
                Response.Write("$('#viewUsers').addClass(\"active\");");
        %>
    });

    // Setup the validator of the form
    $('#form1').validate({
        highlight: function (element) {
            $(element).closest('div').addClass("f_error");
            $(element).closest('div').parent('div').addClass("error");
        },
        unhighlight: function (element) {
            $(element).closest('div').removeClass("f_error");
            $(element).closest('div').parent('div').removeClass("error");
        },
        errorPlacement: function (error, element) {
            // Not sure why but jquery validator is adding a "display: block" style to the label it inserst.
            error.css({ 'display': 'inline' });
            $(element).closest('div').append(error);
        }
        <%
            // Note: One drawback with the version of the JQuery UI of adding the rules like this, is that they don't automatically go away
            // when the user corrects the form. Need to see if need to use additional parameter to fix
            if (OperationMode == Mode.AddUser)
            { 
               Response.Write(@",
                    rules: { 
                        ctl00$ContentPlaceHolder1$AddEditUserFormView$Password: { required: true, minlength: 4 }, 
                        ctl00$ContentPlaceHolder1$AddEditUserFormView$ConfirmPassword: { equalTo: '#Password' }
                    }, 
                    messages: { 
                        ctl00$ContentPlaceHolder1$AddEditUserFormView$ConfirmPassword: { equalTo: 'Must match password.' } 
                    } 
                ");
            }
        %>
    });
    </script>
</asp:Content>
